home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8074 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: need for function prototypes
  5. Date: 29 Feb 1996 13:42:22 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4h56juINNmd0@anvil.ugrad.cs.ubc.ca>
  8. References: <31333401.7D03@stat.uni-muenchen.de>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <31333401.7D03@stat.uni-muenchen.de>,
  12. watzka  <watzka@stat.uni-muenchen.de> wrote:
  13. >Chris Rossall wrote:
  14. >> 
  15. >> Hello I am having trouble understanding why I should use function
  16. >> prototypes. I mean,if the function is defined before it is used,the
  17. >> compiler should have enough information about the parameters to
  18. >> produce correct code anyway.
  19. >
  20. >Yes, indeed, a function definition provides enough information to
  21. >produce correct code. If your function returns an int and has no
  22. >variable argument list, correct code can be produced without a
  23. >function prototype.
  24.  
  25. The function definition serves as a prototype, when you are ``down wind'' in
  26. the scope---that is, if the function you are calling is defined before the
  27. place where you are calling it. This is not a case of not having a prototype.
  28. Within a single translation unit, a prototype serves analogously to a Pascal
  29. ``forward reference'', which is one answer that we can give to Chris Rossall:
  30.  
  31. If you have two mutually recursive functions, you can't avoid using a
  32. prototype (unless you correctly take advantage of the argument promotion rules
  33. that are in effect when calling unprototyped functions):
  34.  
  35.     float a(float x) { /* calls b()    */ }
  36.  
  37.     float b(float x) { /* calls a()    */ }
  38.  
  39. Here, a *requires* a prototype of b, from which it can infer the return value
  40. and the parameter type.  Even if you rewrote them in old-style C, you would
  41. still need a prior declaration to get the return type right.
  42.  
  43. >Function prototypes are useful for _checking_ code, esp. in situations
  44. >where the first call to a function in a translation unit will precede
  45. >the definition of that function.
  46.  
  47. Correct code is produced, but not necessarily a correct calling sequence if no
  48. definition or correct prototype declaration for the called function is found
  49. prior to the inocation.  If a call (any call, not just the first) precedes the
  50. definition of the function, an incorrect calling sequence will be generated,
  51. and undefined behavior will result. In the above example, when a invokes b, a
  52. default prototype is implicitly generated which is ``int b()''. The call to
  53. b() cannot be correct because b() returns a float, not an int. Furthermore,
  54. since b() is not prototyped, if a() passes a float argument to b(), it will be
  55. promoted to a double in the calling sequence. However, b() is a new-style
  56. function which means that the types of arguments and parameters have to match
  57. exactly; i.e. it expects a float.
  58. -- 
  59.  
  60.